Data Analysis by R

Reachability Matrix by R

The reachability matrix is used in the ISM .

If the matrix is made by a simple function, and it would be nice, but there seems to be no such function. So I made it myself.

Calculation

There are three ways to calculate the product of matrices in R, but the "logical product of matrices" required to calculate the reachable matrix is not in the standard library. "Logical product of matrices" can be done with "%&%", but to do this, you need a library called "Matrix".

The Matrix library doesn't have the matrix format you want the output, so the code below also modifies it to the format you want.

The first adjacency matrix before finding the reachability matrix is:
ReachabilityMatrix

Suppose there is a file called Data.csv with the above contents in a folder called Rtest directly under the C drive.

setwd("C:/Rtest")
library(Matrix)
Data <- read.csv("Data.csv", header=T,row.names=1)
AdjacencyMatrix <- as.matrix(Data)
n <- ncol(AdjacencyMatrix)
AdjacencyMatrixI <- AdjacencyMatrix + diag(n)
AdjacencyMatrixI2 <- AdjacencyMatrixI
ReachabilityMatrix <- AdjacencyMatrix
while(sum(ReachabilityMatrix - AdjacencyMatrixI2) != 0){
ReachabilityMatrix <- as.matrix(AdjacencyMatrixI2 %&% AdjacencyMatrixI)*1
AdjacencyMatrixI2 <- ReachabilityMatrix
}
ReachabilityMatrix

The obtained matrix is as follows.
ReachabilityMatrix



Reference

CRAN
How to use "%&%"
https://cran.r-project.org/web/packages/Matrix/Matrix.pdf



Tweet